//
//  XMN_LeftViewController.m
//  B段项目
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "XMN_LeftViewController.h"
#import "SHD_MainViewController.h"
#import "XMN_ReadViewController.h"
#import "MenuTableViewCell.h"
#import "XMN_ DistrictViewController.h"
#import "GZ_RadioViewController.h"
#import "SHD_ChipViewController.h"
#import "SHD_ExcellectViewController.h"
#import "GZ_SearchViewController.h"
#import "XMN_SetingViewController.h"
#import "XMN_DownViewController.h"
#import "XMN_CollectionViewController.h"
#import "XMN_LikeViewController.h"
#import "Music.h"
@interface XMN_LeftViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UIView *backView;
@property(nonatomic, strong)UIImageView *topImageView;
@property(nonatomic, strong)UIImageView *downImageView;
@property(nonatomic, strong)UIImageView *collentImageView;
@property(nonatomic, strong)UIImageView *likeImageView;
@property(nonatomic, strong)UIImageView *searchImageView;
@property(nonatomic, strong)UITableView *menuTableView;
@property(nonatomic, strong)NSArray *dataArray;
@property(nonatomic, strong)NSArray *imageArray;
@property(nonatomic, strong)UIViewController *rootVC;
@property(nonatomic, strong)UIView *playerView;
@property(nonatomic, strong)UIButton *playerButton;
@property(nonatomic, assign)NSInteger playerFlag;
@property(nonatomic, strong)UIImageView *songImageView;
@property(nonatomic, strong)UILabel *songNameLabel;
@property(nonatomic, strong)CABasicAnimation *basicAnimation2;

@end

@implementation XMN_LeftViewController

-(void)loadView{
    [super loadView];
    
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 170 * FitHeight)];
    _backView.userInteractionEnabled = YES;
    [_backView NightWithType:UIViewColorType1];
    [self.view addSubview:_backView];
    
    _topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(30 * FitWidth, 70 * FitHeight, 80 * FitWidth, 80 * FitHeight)];
    _topImageView.layer.masksToBounds = YES;
    _topImageView.layer.cornerRadius = 40 * FitWidth;
    _topImageView.image = [UIImage imageNamed:@"XMN_Main"];
    [self.view addSubview:_topImageView];
    
    UILabel *topLabel = [[UILabel alloc]initWithFrame:CGRectMake(_topImageView.frame.origin.x + 20 * FitWidth + _topImageView.frame.size.width, 40 * FitHeight, 170 * FitWidth, 60 * FitHeight)];
    topLabel.textColor = [UIColor whiteColor];
    topLabel.text = @"❀ 花开半夏 , \n        静默留声 ... ❀";
    topLabel.font = [UIFont systemFontOfSize:18 * FitWidth];
    topLabel.numberOfLines = 0;
    [_backView addSubview:topLabel];
    
    
    _collentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_topImageView.frame.origin.x + _topImageView.frame.size.width + 20 * FitWidth, _topImageView.frame.origin.y + 55 * FitHeight, 25 * FitWidth, 25 * FitHeight)];
    _collentImageView.image = [UIImage imageNamed:@"shoucang"];
    _collentImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *collentTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(collentTapAction)];
    [_collentImageView addGestureRecognizer:collentTap];
    [self.view addSubview:_collentImageView];
    
    _likeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_collentImageView.frame.origin.x + _collentImageView.frame.size.width + 30 * FitWidth, _collentImageView.frame.origin.y, _collentImageView.frame.size.width, _collentImageView.frame.size.height)];
    _likeImageView.image = [UIImage imageNamed:@"like"];
    _likeImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *likeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(likeTapAction)];
    [_likeImageView addGestureRecognizer:likeTap];
    [self.view addSubview:_likeImageView];
    
    _searchImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_likeImageView.frame.origin.x + _likeImageView.frame.size.width + 30 * FitWidth, _likeImageView.frame.origin.y, _likeImageView.frame.size.width, _likeImageView.frame.size.height)];
    _searchImageView.image = [UIImage imageNamed:@"sousuo"];
    _searchImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *searchTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(searchTapAction)];
    [_searchImageView addGestureRecognizer:searchTap];
    [self.view addSubview:_searchImageView];
    
    //初始化数组
    _dataArray = [NSArray arrayWithObjects:@"首页", @"电台", @"阅读", @"社区", @"碎片", @"良品", @"设置", nil];
    _imageArray = [NSArray arrayWithObjects:@"XMN_shouye", @"XMN_diantai", @"XMN_yuedu", @"XMN_shequ", @"XMN_suipian", @"XMN_liangpin", @"XMN_shezhi", nil];
    
    //初始化menuTableView
    _menuTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _backView.frame.origin.y +_backView.frame.size.height + 40 * FitHeight, ScreenWidth, 350 * FitHeight) style:UITableViewStylePlain];
    _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _menuTableView.delegate = self;
    _menuTableView.dataSource = self;
    _menuTableView.bounces = NO;
    [_menuTableView NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:_menuTableView];
    
    
    //播放电台页面
    _playerView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - 70 * FitHeight, ScreenWidth, 70 * FitHeight)];
    [_playerView NightWithType:UIViewColorType1];
    [self.view addSubview:_playerView];
    
    //注册-------------------------------
    
    
    _songImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20 * FitWidth, 10 * FitHeight, 50 * FitWidth, 50 * FitWidth)];
    _songImageView.layer.cornerRadius = 25 * FitHeight;
    _songImageView.layer.masksToBounds = YES;
    _songImageView.image = [UIImage imageNamed:@"GZzhanwei.png"];
    [_playerView addSubview:_songImageView];
  //  _songImageView.frame.size.width + _songImageView.frame.origin.x + 10 * FitWidth
    _songNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_songImageView.frame.size.width + _songImageView.frame.origin.x, _songImageView.frame.origin.y, 180 * FitWidth, _songImageView.frame.size.height)];
    _songNameLabel.textColor = [UIColor whiteColor];
    _songNameLabel.font = [UIFont systemFontOfSize:17 * FitWidth];
    _songNameLabel.textAlignment = NSTextAlignmentCenter;
    _songNameLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    _songNameLabel.text =[NSString stringWithFormat:@"『 未有播放电台 』"];
    [_playerView addSubview:_songNameLabel];
    
    
    
    _playerFlag = 0;
    _playerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _playerButton.frame = CGRectMake(150 * FitWidth + 80 * FitWidth + 20 * FitWidth, 20 * FitHeight, 30 * FitWidth, 30 * FitHeight);
    [_playerButton setImage:[UIImage imageNamed:@"GZzanting.png"] forState:UIControlStateNormal];
    _playerButton.tintColor = [UIColor whiteColor];
    [_playerButton addTarget:self action:@selector(playerButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [_playerView addSubview:_playerButton];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeImage:) name:@"songBGimageView" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeName:) name:@"songName" object:nil];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - tap手势触发事件
-(void)downTapAction{
    //找到根视图
    DDMenuController *menu = (DDMenuController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    _rootVC = [[XMN_DownViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:_rootVC];
    [menu setRootController:nav animated:YES];
}

-(void)collentTapAction{
    //找到根视图
    DDMenuController *menu = (DDMenuController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    _rootVC = [[XMN_CollectionViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:_rootVC];
    [menu setRootController:nav animated:YES];
}

-(void)likeTapAction{
    //找到根视图
    DDMenuController *menu = (DDMenuController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    _rootVC = [[XMN_LikeViewController alloc]init];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:_rootVC];
    [menu setRootController:nav1 animated:YES];
}

-(void)searchTapAction{
     DDMenuController *menu = (DDMenuController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    _rootVC = [[GZ_SearchViewController alloc] init];
    
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:_rootVC];
    [menu setRootController:nav1 animated:YES];
}

#pragma mark - menuTableView相关设置
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"menuCell";
    MenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[MenuTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        [cell NightWithType:UIViewColorType1];
    }
    cell.leftImageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
    cell.rightLabel.text = [_dataArray objectAtIndex:indexPath.row];
    //tableView点击样式
    UIView *cellBackView = [[UIView alloc]initWithFrame:cell.frame];
    cellBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.2];
    cell.selectedBackgroundView = cellBackView;

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50 * FitHeight;
}

#pragma mark menuTableView点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //找到根视图
    DDMenuController *menu = (DDMenuController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    switch (indexPath.row) {
        case 0:{
            _rootVC = [[SHD_MainViewController alloc]init];
            break;
        }
        case 1:{
            _rootVC = [[GZ_RadioViewController alloc]init];
            break;
        }
        case 2:{
            _rootVC = [[XMN_ReadViewController alloc]init];
            break;
        }
        case 3:{
            _rootVC = [[XMN__DistrictViewController alloc]init];
            break;
        }
        case 4:{
            _rootVC = [[SHD_ChipViewController alloc]init];
            break;
        }
        case 5:{
            _rootVC = [[SHD_ExcellectViewController alloc]init];
            break;
        }
        case 6:{
            _rootVC = [[XMN_SetingViewController alloc]init];
            break;
        }
    }
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:_rootVC];
    [menu setRootController:nav1 animated:YES];
}

- (void)playerButtonAction
{
    
    if (_playerFlag == 0) {
        [_playerButton setImage:[UIImage imageNamed:@"GZplay.png"] forState:UIControlStateNormal];
        _playerFlag = 1;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"sendFlag" object:[NSNumber numberWithInteger:0]];
         [_songImageView.layer removeAnimationForKey:@"rotation"];
    }else{
        [_playerButton setImage:[UIImage imageNamed:@"GZzanting.png"] forState:UIControlStateNormal];
        _playerFlag = 0;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"sendFlag" object:[NSNumber numberWithInteger:1]];
         [_songImageView.layer addAnimation:_basicAnimation2 forKey:@"rotation"];
    }
    
    
}
- (void)changeImage:(NSNotification *)notification
{
    [_songImageView sd_setImageWithURL:[notification object]];
   }
- (void)changeName:(NSNotification *)notification
{
    _songNameLabel.text =[NSString stringWithFormat:@"『 %@ 』",[notification object]];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated
{
    _basicAnimation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    _basicAnimation2.fromValue = [NSNumber numberWithInteger:0];
    _basicAnimation2.toValue = [NSNumber numberWithInteger:M_PI * 100];
    [_basicAnimation2 setDuration:900];
    [_basicAnimation2 setRepeatCount:NSIntegerMax];
    [_songImageView.layer addAnimation:_basicAnimation2 forKey:@"rotation"];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Music"]) {
        [_playerButton setImage:[UIImage imageNamed:@"GZzanting.png"] forState:UIControlStateNormal];
    }
    else{
        [_playerButton setImage:[UIImage imageNamed:@"GZplay.png"] forState:UIControlStateNormal];
        
    }
    

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
